package com.cby.android.sample.fragments

import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.cby.android.library.base.BaseStateFragment
import com.cby.android.library.base.callback.*
import com.cby.android.library.ext.observe
import com.cby.android.library.ext.showToast
import com.cby.android.sample.R
import com.cby.android.sample.adapter.MetaImageAdapter
import com.cby.android.sample.constant.MetaConstant
import com.cby.android.sample.intent.MetaImageIntent
import com.cby.android.sample.model.MetaImageViewModel
import com.cby.android.sample.state.MetaImageState
import com.kingja.loadsir.core.LoadSir
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import kotlinx.coroutines.launch

/**
 * @description: MetaImageFragment
 * @author: ChenBaoYang
 * @createDate: 2022/4/27 3:04 下午
 */
class MetaImageFragment : BaseStateFragment<MetaImageViewModel, MetaImageState, MetaImageIntent>(), OnLoadMoreListener, OnRefreshListener {

    private val recyclerView: RecyclerView? by lazy { findViewById(R.id.swipe_target) }
    private val refreshLayout: RefreshLayout? by lazy { findViewById(R.id.refreshLayout) }
    private val keywordEdit: EditText? by lazy { findViewById(R.id.edit_keyword) }
    private val searchBtn: Button? by lazy { findViewById(R.id.btn_search) }
    private val changeBtn: Button? by lazy { findViewById(R.id.btn_change) }

    private lateinit var imageAdapter: MetaImageAdapter

    override fun getLayoutId() = R.layout.meta_fragment_image

    override fun onViewInit(view: View) {
        imageAdapter = MetaImageAdapter(requireActivity())

        recyclerView?.apply {
            layoutManager = LinearLayoutManager(activity)
            setHasFixedSize(true)
            adapter = imageAdapter
        }

        refreshLayout?.setOnLoadMoreListener(this)
        refreshLayout?.setOnRefreshListener(this)

        searchBtn?.setOnClickListener {
            val currentKeyWord = keywordEdit?.text.toString()
            if (TextUtils.isEmpty(currentKeyWord)) {
                keywordEdit?.error = "不能为空"
                return@setOnClickListener
            }
            viewModel?.apply {
                keyword = currentKeyWord
                lifecycleScope.launch {
                    intents.send(MetaImageIntent.LoadDataIntent)
                }
            }
        }

        changeBtn?.setOnClickListener {
            when (imageAdapter.currentType) {
                MetaConstant.META_IMAGE_TYPE_LINEAR -> {
                    recyclerView?.layoutManager = GridLayoutManager(activity, 2)
                    imageAdapter.updateImageShowType(MetaConstant.META_IMAGE_TYPE_GRID)
                }

                MetaConstant.META_IMAGE_TYPE_GRID -> {
                    recyclerView?.layoutManager =
                        StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
                    imageAdapter.updateImageShowType(MetaConstant.META_IMAGE_TYPE_STAGE)
                }

                MetaConstant.META_IMAGE_TYPE_STAGE -> {
                    recyclerView?.layoutManager = LinearLayoutManager(activity)
                    imageAdapter.updateImageShowType(MetaConstant.META_IMAGE_TYPE_LINEAR)
                }
            }
        }


    }

    override fun loadData() {

    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        lifecycleScope.launch {
            viewModel?.intents?.send(MetaImageIntent.LoadMoreDataIntent)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        lifecycleScope.launch {
            viewModel?.intents?.send(MetaImageIntent.LoadDataIntent)
        }
    }

    override fun bindLiveData() {
        viewModel?.let {
            observe(it.state) { it1->
                refreshLayout?.finishLoadMore(true)
                refreshLayout?.finishRefresh(true)

                if (!TextUtils.isEmpty(it1.errorMsg)) {
                    showToast(it1.errorMsg!!)
                    return@observe
                }
                if(it1.page > 0) {
                    imageAdapter.addData(it1.list)
                }else{
                    imageAdapter.setData(it1.list)
                }
            }
        }
    }

    override fun initLoadSir() {
        val loadSir = LoadSir.Builder()
            .addCallback(LottieLoadingCallback())
            .addCallback(EmptyCallback())
            .addCallback(ErrorCallback())
            .addCallback(TimeoutCallback())
            .setDefaultCallback(EmptyCallback::class.java)
            .build()
        loadService = loadSir.register(refreshLayout){
            reload()
        }
    }

    override fun reload() {
        lifecycleScope.launch {
            viewModel?.intents?.send(MetaImageIntent.LoadDataIntent)
        }
    }

}